#!/bin/bash
#tomcat path : /home/tomcat/{app1,app2,app3}
#Usage:
#  ./server app_id [start|status|stop|restart|tailf]
# Ctrl+C == Exit
trap '' SIGTSTP

export tomcat_path=/home/tomcat
export app_id=${1}
export app_status=${2}
export suM=`ps -ef|grep /${app_id}/|grep java|grep -v grep |wc -l`

tail_log_server (){
  tail -f ${tomcat_path}/${app_id}/logs/catalina.out
}

stop_server (){
  pid=`ps -ef|grep /${app_id}/|grep java|grep -v grep|awk '{print $2}'`
  if [ ! -n "$pid" ]; then
    echo -e "\033[33mserver ${app_id} not running.\033[0m"
  else
    pid_k=`ps -ef|grep /${app_id}/|grep java|grep -v grep|awk '{print $2}' | xargs kill -9`
    echo -e -n "Stopped server \033[1m\033[32m${app_id}\033[0m"
    echo -e -n " : \033[1m\033[32m${app_id}\033[0m has be killed, "
    echo -e -n "PID is { \033[1m\033[32m${pid}\033[0m }, "
    echo -e "kill process count \033[1m\033[32m${suM}\033[0m"
  fi
}

start_server (){
  cd ${tomcat_path}/${app_id}
  rm -rf ${tomcat_path}/${app_id}/work/Catalina/
  ./bin/startup.sh > /dev/null
  pid=`ps -ef|grep /${app_id}/|grep java|grep -v grep|awk '{print $2}'`
  echo -e -n "Started server \033[1m\033[32m${app_id}\033[0m"
  echo -e " : PID is { \033[1m\033[32m${pid}\033[0m }. "
}

restart_server (){
  stop_server
  start_server
  
  echo -e '请按\033[32;1m[回车]\033[0m键继续查看日志或使用[\033[32;1mCtrl+c\033[0m]退出.......'
  read
  tail_log_server
}

silent_restart_server (){
  stop_server
  start_server
}

status_server (){
  server_pid=$(jps -mv | grep -v Jps | grep /${app_id}/ | awk '{print $1,$2,$3}')
  server_path=$(jps -mvlV | grep -v Jps | grep /${app_id}/ | awk  '{print $(NF-2)}')
  if [[ -n ${server_pid} ]];then 
    runningTime=$(ps -o etime=${server_id} | tail -n 1)
    echo -e "Pid: \033[32m${server_pid}\033[0m | path: \033[32m${server_path}\033[0m | Run Time \033[32m${runningTime}\033[0m"
  else
    echo -e "\033[33mserver ${app_id} not running \033[0m" | tr '\n' ','
    echo " Usage: ./server tomcat [start|status|stop|restart]"
  fi
}

check_start (){
  if [[ ${suM} -ge 1 ]];then
    echo -e "Server \033[1m\033[32m[${app_id}]\033[0m is started."
  else
    start_server ${app_id}
  fi
}

if [[ ! -d ${tomcat_path}/${app_id} ]];then 
   echo  "Server [${app_id}] not exites" | tr '\n' ','
   ls_dir=$(ls -l ${tomcat_path} | grep "^d" | awk '{print $NF}')
   echo "Please choose [${ls_dir}]" | tr '\n' ','
   echo "Exit."
   exit 2
else 
   tomcat_server=$(ps -ef | grep ${tomcat_path}/${app_id}/bin | grep Djava | grep -v grep | awk '{print $1}')
   if [[ ${suM} -lt 1 || ($2 = "restart") || ($2 = "slient-restart") ]];then
      :
   else
     if [[ $USER != ${tomcat_server} ]];then 
        echo "Please su [${tomcat_server}]."
        exit 3
     fi
   fi
fi

case ${app_status} in
 stop)
   stop_server
 ;;
 start)
   check_start
 ;;
 restart)
   restart_server
 ;;
 slient-restart)
   silent_restart_server
 ;;
 status)
   status_server
 ;;
 logs)
   tail_log_server
 ;;
 *)
   echo "Usage: ./$0 tomcat [start|status|stop|restart|slient-restart|logs]"
 ;;
esac
